const Joi = require('joi');

const validate = (schema) => {
  return (req, res, next) => {
    const { error } = schema.validate(req.body);
    if (error) {
      return res.status(400).json({
        error: error.details[0].message
      });
    }
    next();
  };
};

const schemas = {
  course: Joi.object({
    name: Joi.string().required().min(2).max(100),
    teacher: Joi.string().allow(''),
    place: Joi.string().allow(''),
    weekDay: Joi.number().required().min(1).max(7),
    startTime: Joi.number().required().min(1).max(14),
    endTime: Joi.number().required().min(1).max(14)
      .greater(Joi.ref('startTime'))
  }),

  event: Joi.object({
    courseId: Joi.string().required(),
    title: Joi.string().required().min(2).max(100),
    description: Joi.string().allow(''),
    endDate: Joi.date().required().greater('now')
  }),

  class: Joi.object({
    name: Joi.string().required().min(2).max(100)
  })
};

module.exports = {
  validate,
  schemas
};